<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Backend_Pages extends Controller_Backend {
	
	public function action_index()
	{
		$view = new View('backend/pages.index');
		
		$columns = ORM::factory('Page')->table_columns();
		$view->columns = $columns;
		
		$pages = ORM::factory('Page')->order_by('id', 'asc')->find_all();
		$view->pages = $pages;
		
		$this->template->content = $view;
	}
	
	public function action_edit()
	{
		$view = new View('backend/pages.edit');
		
		$id = Request::current()->param('par1');
		
		$columns = ORM::factory('Page')->table_columns();
		$view->columns = $columns;
		
		$page = ORM::factory('Page')->where('id', '=', $id)->find();
		$view->page = $page;
		
		if (isset($_POST['page']))
		{
			$page->values($_POST['page'])->save();
			
			if (isset($_POST['save_and_return']))
				Request::current()->redirect(Route::get('backend')->uri(array('controller' => 'pages')));
			else
				Request::current()->redirect(Route::get('backend')->uri(array('controller' => 'pages', 'action' => 'edit', 'par1' => $page->id)));
		}
		
		$this->template->content = $view;
	}
	
	public function action_delete()
	{
		$id = Request::current()->param('par1');
		ORM::factory('Page')->where('id', '=', $id)->find()->delete();
		Request::current()->redirect(Route::get('backend')->uri(array('controller' => 'pages')));
	}
	
}